package com.example.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottom;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        HomeFragment homeFragmet = new HomeFragment();
        ServicesFragment servicesFragment = new ServicesFragment();
        ActivityFragment activityFragment = new ActivityFragment();
        New1Fragment new1Fragment  = new New1Fragment();
        PersonFragment personFragment = new PersonFragment();

        getSupportFragmentManager().beginTransaction().add(R.id.layMain, homeFragmet).commit();
        last = homeFragmet;

        bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        add(homeFragmet);
                        Toast.makeText(MainActivity.this, "首页", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.services:
                        add(servicesFragment);
                        Toast.makeText(MainActivity.this, "全部服务", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.activity:
                        add(activityFragment);
                        Toast.makeText(MainActivity.this, "党建活动", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.new1:
                        add(new1Fragment);
                        Toast.makeText(MainActivity.this, "新闻", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.person:
                        add(personFragment);
                        Toast.makeText(MainActivity.this, "个人中心", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });

    }

    Fragment last;

    private void add(Fragment fragment) {
        if (fragment == last) return;
        if (fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().show(fragment).hide(last).commit();
        } else {
            getSupportFragmentManager().beginTransaction().add(R.id.layMain, fragment).hide(last).commit();
        }
        last = fragment;
    }

    private void initView() {

        bottom = (BottomNavigationView) findViewById(R.id.bottom);
    }
}